// pages/user-course/user-course-detail/course-appoint/course-coach/course-coach.js
const app = getApp()
const constant = require("../../../../../constant.js")
const storage = require("../../../../../utils/storage.js")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    disabled: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      qiniu: constant.resource.qiniu,
      host: constant.api(app.globalData.pord).url,
      unionid: wx.getStorageSync(storage.keys.unionid),
      FU_XING: constant.resource.FU_XING,
      courseRoundId: options.courseRoundId,
      siteId: options.siteId,
      occurDate: options.occurDate,
      occurHour: options.occurHour
    });

    if(options.scheduleId) {
      this.setData({
        scheduleId: options.scheduleId
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.showLoading({
      title: "正在加载中..."
    });

    var that = this;

    wx.request({
      url: that.data.host + '/profile/courses/' + that.data.courseRoundId + '/coaches?unionid=' + that.data.unionid + '&siteId=' + that.data.siteId + '&occurDate=' + that.data.occurDate + '&occurHour=' + that.data.occurHour + '&scheduleId=' + (that.data.scheduleId ? that.data.scheduleId : ''),
      method: 'GET',
      dataType: 'json',
      responseType: 'text',
      success: function (res) {
        var data = res.data.data
        if (res.data.code == "0") {
          that.setData({
            siteName: res.data.data.siteName,
            coaches: res.data.data.coaches,
            roomLimit: res.data.data.roomLimit,
            commonLimit: res.data.data.commonLimit
          });
          
          if (that.data.roomLimit <= 0 && that.data.commonLimit > 0) {
            that.setData({
              roomType: "COMMON"
            });
          } else if (that.data.roomLimit > 0 && that.data.commonLimit <= 0) {
            that.setData({
              roomType: "ROOM"
            });
          }

          wx.hideLoading();
        } else{
          wx.hideLoading();

          wx.showToast({
            title: res.data.message ? res.data.message : '请求失败',
            icon: 'none',
            duration: 1000
          })
        }
      },
      fail: function (res) {
        wx.hideLoading();

        wx.showToast({
          title: '请求数据失败',
          icon: 'none',
          duration: 1000
        });
      }
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  formSubmit: function(e) {
    var that = this;
    that.setData({
      disabled: true,
      coachId: e.detail.target.dataset.id
    })

    that.onSubmit();
  },

  onSubmit: function() {
    var that = this;
    
    wx.request({
      url: that.data.host + '/profile/courses/' + that.data.courseRoundId + '/appoint?unionid=' + that.data.unionid + '&scheduleId=' + (that.data.scheduleId ? that.data.scheduleId : ''),
      method: 'POST',
      data: {
        site_id: that.data.siteId,
        coach_id: that.data.coachId,
        occur_date: that.data.occurDate,
        occur_hour: that.data.occurHour
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        if (res.data.code == "0") {
          wx.showToast({
            title: res.data.message,
            icon: 'success',
            duration: 1000,
            mask: true,
            success: function() {
              setTimeout(function () {
                wx.navigateBack({
                  delta: 2
                });
              }, 1000)
            }
          });
        } else {
          that.setData({
            disabled: false
          })
          wx.showToast({
            title: res.data.message ? res.data.message : '请求失败',
            icon: 'none',
            duration: 3000,
            mask: true
          })
        }
      },
      fail: function (res) {
        that.setData({
          disabled: false
        })
        wx.showToast({
          title: '预约失败',
          icon: 'none',
          duration: 3000,
          mask: true
        })
      }
    });
  }
})